package com.test;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.TimeUnit;

/**
 * Created by mike on 17/8/27.
 * Thread-PerMessage模式实现的服务器
 */
public class TPMWebServer {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(8888);
        while (true) {
            Socket socket = ss.accept();
            Client.handle();
        }
    }

    static class Client {
        static void handle() {
            Host.request();
        }
    }

    static class Host {
        static void request() {
            new Thread(new Runnable() {
                @Override
                public void run() {

                    Helper.handle();
                }
            }).start();
        }
    }

    static class Helper {
        static void handle() {
            for (int i = 0; i < 10; i++) {
                System.out.println(Thread.currentThread().getName() + " is:" + i);
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
